package org.domon.attendance;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * [文件名称]<br>
 * ExcelUtil <br>
 * <br>
 * [文件描述]<br>
 * 基于POI的excel操作<br>
 * <br>
 * [修改记录]<br>
 * 2012-5-31 ver1.00 创建 xyl<br>
 * 
 * @author xyl
 * @version 1.00
 */
public class ExcelUtil {

	private HSSFWorkbook workbook;
  
	public HSSFWorkbook createExcelStart() {
		workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("全量数据");
		sheet.setColumnWidth(3, 6000);
		sheet.setColumnWidth(4, 3000);
		sheet.setColumnWidth(5, 3000);
		sheet.setColumnWidth(6, 5000);

		HSSFRow row = sheet.createRow(0);// 创建一行
		HSSFFont font = workbook.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFont(font);
		row.setRowStyle(cellStyle);

		HSSFCell cell = row.createCell(0);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue("部门");

		cell = row.createCell(1);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue("姓名");

		cell = row.createCell(2);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue("登记号码");

		cell = row.createCell(3);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue("日期时间");

		cell = row.createCell(4);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue("机器号");

		cell = row.createCell(5);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue("CardNo");

		cell = row.createCell(6);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue("类型");

		return workbook;
	}

	public void writeExcel(int rowNo, Worker worker, String type) {
		HSSFSheet sheet = workbook.getSheetAt(0);
		HSSFRow row = sheet.getRow(rowNo) == null ? sheet.createRow(rowNo)
				: sheet.getRow(rowNo);

		HSSFCell cell = row.createCell(0);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue(worker.getDept());

		cell = row.createCell(1);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue(worker.getName());

		cell = row.createCell(2);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue(worker.getId());

		cell = row.createCell(3);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		if (worker.getDate() == null) {
			cell.setCellValue("");
		} else {
			cell.setCellValue(FastDateFormat.getInstance("yyyy-M-d a h:mm:ss")
					.format(worker.getDate()));
		}

		cell = row.createCell(4);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue(worker.getStbNo());

		cell = row.createCell(5);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue(worker.getCardNo());

		cell = row.createCell(6);// 创建一列
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue(type);

	}

	public void createExcelEnd(String fileName) throws IOException {
		OutputStream os = null;
		try {
			File directory = new File(fileName);
			int i = 1;
			while (directory.isFile()) {
				String newFileName = fileName.substring(0,
						fileName.indexOf("."))
						+ "("
						+ (i++)
						+ ")"
						+ fileName.substring(fileName.indexOf("."));
				directory = new File(newFileName);
			}
			os = new FileOutputStream(directory);
			workbook.write(os);
		} finally {
			if (os != null) {
				os.close();
			}
		}

	}

	public static void main(String[] args) throws Exception {
		ExcelUtil e = new ExcelUtil();
		e.createExcelStart();
		Worker w = new Worker();
		w.setId("1");
		w.setName("a1");
		w.setDate(new Date());
		e.writeExcel(1, w, "sss");
		e.writeExcel(3, w, "sss");
		e.createExcelEnd("xxxx.xls");
	}
}
